<template>
  <el-container class="home-container">
<!--    头部部分-->
    <el-header>
      <div>
        <img src="../assets/logo.png" alt="Shop Log">
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">
        Log out
      </el-button>
    </el-header>
    <el-container>
<!--      侧边栏部分-->
      <el-aside :width="isCollapsed?'64px':'200px'">
<!--      为盒子添加点击事件 -->
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu
          background-color="#323744"
          text-color="#fff"
          active-text-color="#60bdec"
          unique-opened
          :collapse="isCollapsed"
          :collapse-transition="false"
          router
          :default-active="activeIndex">
<!--          一级菜单-->
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
<!--            一级菜单模板部分-->
            <template slot="title">
<!--              图标-->
              <i :class="iconList[item.id]"></i>
<!--              文本-->
              <span>{{ item.authName }}</span>
            </template>
<!--            二级菜单-->
            <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id"
              @click="saveIndex('/'+subItem.path)">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
<!--      页面主体部分-->
      <el-main>
<!--        放置Main组件的子组件Welcome的占位符-->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'Home',
  data () {
    return {
      // 左侧菜单栏数据
      menuList: [],
      // 左侧一级菜单图标名
      iconList: {
        125: 'el-icon-user-solid',
        103: 'el-icon-s-tools',
        101: 'el-icon-s-goods',
        102: 'el-icon-s-order',
        145: 'el-icon-s-marketing'
      },
      // 控制导航栏是否折叠
      isCollapsed: false,
      // 控制二级菜单的高亮
      activeIndex: ''
    }
  },
  // 生命周期函数
  created () {
    this.getMenuList()
    // 页面创建完成，从sessionStorage中获取高亮index
    this.activeIndex = window.sessionStorage.getItem('activeIndex')
  },
  methods: {
    // 注销
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取左侧菜单数据
    async getMenuList () {
      // 运用结构赋值
      const { data: res } = await this.$http('menus')
      // 若请求失败，弹出报错信息，并返回
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      console.log(res)
      this.menuList = res.data
    },
    // 左侧侧边导航栏点击切换折叠效果
    toggleCollapse () {
      this.isCollapsed = !this.isCollapsed
    },
    saveIndex (activeIndex) {
      // 点击二级菜单，保存index至sessionStorage中
      window.sessionStorage.setItem('activeIndex', activeIndex)
      // 立即将该二级菜单高亮
      this.activeIndex = activeIndex
    }
  }
}
</script>

<style scoped lang="less">
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373c41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  div {
    display: flex;
    align-items: center;
    img {
      width: 60px;
      height: 60px;
    }
    span {
      color: #fff;
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #323744;
  .toggle-button {
    background-color: #4A5064;
    font-size: 10px;
    color: #fff;
    line-height: 24px;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
  }
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #eaedf2;
}

</style>
